package threadDemo;

import org.apache.log4j.Logger;

/**
 * @author Ajie
 * @date 7/20/21
 * @function 线程状态 研究
 * <p>
 * 接下来来看看 Java 中线程的生命周期，Java 中线程共有六种状态，分别是：
 * <p>
 * 1.NEW（初始化状态）
 * 2.RUNNABLE（可运行 / 运行状态）
 * 3.BLOCKED（阻塞状态）synchronized  2个线程抢锁。
 * 4.WAITING（无时限等待） Thread.sleep
 * 5.TIMED_WAITING（有时限等待）
 * 6.TERMINATED（终止状态）
 * <p>
 * 在操作系统层面，Java 线程中的 BLOCKED（阻塞状态）、WAITING（无时限等待）、TIMED_WAITING（有时限等待）
 * 是一种状态，即休眠状态。
 * 也就是说只要 Java 线程处于这三种状态之一，那么这个线程就永远没有 CPU 的使用权。
 * https://mp.weixin.qq.com/s?__biz=MzU4Mjk0MjkxNA==&mid=2247484138&idx=1&sn=11835694e2acc56ce177288b32c992e1&scene=21#wechat_redirect
 * <p>
 * 详情看，xmind
 *
 *
 */
public class ThreadLifeCycleDemo {
    private static final Logger logger = Logger.getLogger(ThreadLifeCycleDemo.class);

    public static void main(String[] args) {
        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                logger.info("run: start task");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                logger.info("run: end task 1");

                try {
                    synchronized (ThreadLifeCycleDemo.class) {
                        logger.info("run: AAAA  sync get lock");
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });

        thread.start();
        logger.error("main: getState:" + thread.getState());

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {


                synchronized (ThreadLifeCycleDemo.class) {
                    try {
                        logger.error("B run: A getState:" + thread.getState());//TODO 开始 A 是TIMED_WAITING ,等一会儿就Blocked 了
                        logger.info("run: BBBBBB  sync get lock");
                        Thread.sleep(100);
                        logger.error("B run: A 100 getState:" + thread.getState());//TODO TIMED_WAITING
                        Thread.sleep(100);
                        logger.error("B run: A 200 getState:" + thread.getState());// BLOCKED
                        Thread.sleep(2000);
                        logger.info("run: BBBBBB  sync release lock");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        threadB.start();

        try {
            Thread.sleep(100);
            logger.error("main: 100ms getState:" + thread.getState());//TIMED_WAITING
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.error("main: synchronized getState:" + thread.getState());//TIMED_WAITING
        try {
            Thread.sleep(300);
            logger.error("main: 400ms synchronized getState:" + thread.getState());//TIMED_WAITING
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            Thread.sleep(1000);
            logger.error("main: 1100ms getState:" + thread.getState());//TERMINATED
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
